PRG 1.0 TXT

/*
*/

//-----------------------------------------------------------------------------
// [CONFIG]

include "..\program_include.ocf"

object Program
{
	type		= e_program.DIFFUSE;
	layer_bits	= e_render_layer_bits.VIEW;

	textures	= array /*ProgramTexture*/ {
		{ "unShadow", e_texture.DIFFUSE }
	}
	
	uniforms	= array /*string*/ {
		"transform", "unTransform."
	}
}


//-----------------------------------------------------------------------------
// [VERTEX]

#version 330 core
#pragma optimize(on)

#define ATTR_POSITION	0

layout(location = ATTR_POSITION) in vec3  inPosition;


void main()
{
	gl_Position = vec4( inPosition, 1.0 );
}


//-----------------------------------------------------------------------------
// [GEOMETRY]

#version 330 core
#pragma optimize(on)

layout(triangles) in;
layout(triangle_strip, max_vertices = 24) out;

uniform struct TTransform {
	mat4	MVP;
} unTransform;

out	TGeomData {
	vec3	vTexcoord;
} Output;

/*
Input:
	   1____2
		|  /
		| /
		|/
		0

Output:
y3			0-------6				(1,1)
			|		|
			|	+Y	|
			|		|
y2	0-------2-------4-------6-------0
	|		|		|		|		|
	|	+X	|	+Z	|	-X	|	-Z	|
	|		|		|		|		|
y1	1-------3-------5-------7-------1
			|		|
			|	-Y	|
			|		|
y0			1-------7
	x0		x1		x2		x3		x4

	y
	|_ x
(0,0)
*/

mat3 CubemapFaceMatrix(int iFace)
{
	int		i    = iFace >> 1;
	float	m[9] = float[]( 0.0, 0.0, 0.0,
							0.0, 0.0, 0.0,
							0.0, 0.0, 0.0 );
	m[int(i==0)] = 1.0;
	m[6-i]       = 1.0;
	m[i+6]       = 1.0;
	
	bool(iFace & 1) ? m[i+6] = - m[i+6] : 0.0;

	return mat3( vec3( m[0], m[1], m[2] ),
				 vec3( m[3], m[4], m[5] ),
				 vec3( m[6], m[7], m[8] ) );
}

vec3 ToCubemapFace(in vec2 vTexcoord, int iFace)
{
	return CubemapFaceMatrix( iFace ) * vec3( vTexcoord - vec2(0.5), 0.5 );
}


void Quad(in vec2 vLeftBottom, in vec2 vRightTop, int face)
{
	gl_Position		 = unTransform.MVP * vec4( vLeftBottom.x, vRightTop.y, 0.0, 1.0 );
	Output.vTexcoord = ToCubemapFace( vec2( 0.0, 1.0 ), face );
	EmitVertex();
	
	gl_Position		 = unTransform.MVP * vec4( vLeftBottom, 0.0, 1.0 );
	Output.vTexcoord = ToCubemapFace( vec2( 0.0, 0.0 ), face );
	EmitVertex();
	
	gl_Position		 = unTransform.MVP * vec4( vRightTop, 0.0, 1.0 );
	Output.vTexcoord = ToCubemapFace( vec2( 1.0, 1.0 ), face );
	EmitVertex();

	gl_Position		 = unTransform.MVP * vec4( vRightTop.x, vLeftBottom.y, 0.0, 1.0 );
	Output.vTexcoord = ToCubemapFace( vec2( 1.0, 0.0 ), face );
	EmitVertex();
	
	EndPrimitive();
}


void main()
{
	if ( gl_PrimitiveIDIn != 0 )
		return;
		
	// min, max, step
	float	min_x  = min( gl_in[1].gl_Position.x, gl_in[2].gl_Position.x );
	float	max_x  = max( gl_in[1].gl_Position.x, gl_in[2].gl_Position.x );
	float	min_y  = min( gl_in[0].gl_Position.y, gl_in[1].gl_Position.y );
	float	max_y  = max( gl_in[0].gl_Position.y, gl_in[1].gl_Position.y );
	float	step_x = (max_x - min_x) / 4;
	float	step_y = (max_y - min_y) / 3;
	float	x[5]   = float[]( min_x, min_x + step_x, min_x + 2*step_x, min_x + 3*step_x, max_x );
	float	y[4]   = float[]( min_y, min_y + step_y, min_y + 2*step_y, max_y );

	/*+X*/ Quad( vec2( x[0], y[1] ),  vec2( x[1], y[2] ),  0 );
	/*-X*/ Quad( vec2( x[2], y[1] ),  vec2( x[3], y[2] ),  1 );
	/*+Y*/ Quad( vec2( x[1], y[2] ),  vec2( x[2], y[3] ),  2 );
	/*-Y*/ Quad( vec2( x[1], y[0] ),  vec2( x[2], y[1] ),  3 );
	/*+Z*/ Quad( vec2( x[1], y[1] ),  vec2( x[2], y[2] ),  4 );
	/*-Z*/ Quad( vec2( x[3], y[1] ),  vec2( x[4], y[2] ),  5 );
}


//-----------------------------------------------------------------------------
// [FRAGMENT]

#version 330 core
#pragma optimize(on)

layout(location = 0) out vec4  outColor;

uniform samplerCube		unShadow;

in	TGeomData {
	vec3	vTexcoord;
} Input;


void main()
{
	outColor = texture( unShadow, Input.vTexcoord );
}

// [END]
